<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>排序</title>
    <!--将以下数组50岁以上得人去掉，18岁以下的人去掉，
   显示出来表格,要求递减排序，如果年龄相等就把男性排在前面-->
</head>
<body>
<table id="tbl"></table>
<script>
    var users = [
        {name: "张三", age: 23, sex: '男'},
        {name: "老王", age: 60, sex: '男'},
        {name: "桂香", age: 19, sex: '女'},
        {name: "魏红", age: 33, sex: '女'},
        {name: "张新发", age: 33, sex: '男'},
        {name: "李丽", age: 23, sex: '女'},
        {name: "二狗", age: 5, sex: '男'}
    ];

    users.sort(function (a, b) {//a,b均为对象  年龄按递减排序

        var r = b.age - a.age;
        if (r == 0) {
            return b.sex.charCodeAt(0) - a.sex.charCodeAt(0);
        }
        return r;
    });

   users=users.filter(function(item,index,array){//去掉年龄不符合条件的
        /*debugger;*/
        return item.age<=50&&item.age>=18;
        });

    var str= users.map(function(item,index,array){
            return`<tr>
            <td>${item.name}</td>
            <td>${item.age}</td>
            <td>${item.sex}</td>
            </tr>
            `;
        });
    str=str.reduce(function(qian,dangqian ,index,array){
            return qian+dangqian;
        });
        var tbl=document.getElementById('tbl');
        tbl.innerHTML=str;


</script>
</body>
</html>